package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
       Category category = new Category();
       //将用户传递的数据拷贝到该实体类上
        BeanUtils.copyProperties(categoryDTO,category);
        //将没有属性值的字段进行赋操作,
        //默认新建分类的时候，该按钮是禁用的，不能进行使用
        category.setStatus(StatusConstant.DISABLE);
        //调用CategoryMapper的方法进行访问数据库
        categoryMapper.insertCategory(category);
    }

    @Override
    public PageResult queryByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        //使用插件进行分页查询
        PageHelper.startPage(page,pageSize);
        //调用CategoryMapper的方法连接数据库
        List<Category> categoryList = categoryMapper.queryByPage(categoryPageQueryDTO);
        //将其转化为PageInfo的数据形式
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categoryList);
        //将其封装进入PageResult里面
        PageResult pageResult = new PageResult(categoryPageInfo.getTotal(),categoryPageInfo.getList());
        //将封装的结果进行返回
        return pageResult;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //使用builder进行注解后支持链式调用
        Category category = Category.builder()
                .id(id)
                .status(status)
                .build();
        categoryMapper.updateCategory(category);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        //将用户书写的值重新赋给category实例,更新时间和用户
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> queryByType(Integer type) {
        List<Category> categoryList = categoryMapper.queryByType(type);
        return categoryList;
    }
    //根据id删除分类的时候，要查找该分类是否关联了菜品和套餐，如果有关联，则抛出异常
    @Override
    public void deleteById(Long id) {
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品不能进行删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        count = setmealMapper.countByCategoryId(id);
        if(count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //若都没有关联则可以进行删除
        categoryMapper.deleteById(id);
    }
}
